package com.fdb.efp.esb.service.impl.yed.customer;

import com.alibaba.fastjson.JSON;
import com.fdb.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.constant.EsbRetCodeStandard;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.yed.customer.ReqYedCusToOnlineFinanceBean;
import com.fdb.efp.esb.service.bo.resp.yed.customer.RespYedCusToOnlineFinanceBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import com.fdb.efp.esb.service.facade.yed.customer.YedCustomerCollectionService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * 需求编号:2019D0519
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/7/17
 * 功能描述:客人信息维护 impl
 * 修改日期:2019/7/17
 * 修改描述:
 */
@Service("yedCustomerCollectionService")
public class YedCustomerCollectionServiceImpl implements YedCustomerCollectionService {
    /**
     * @author zhangxc
     * @return
     * @date 2020年8月6日16:00:34
     * @version 0.1
     * @since 2020年8月6日16:00:34
     */
    private static Logger logger = LoggerFactory.getLogger(YedCustomerCollectionServiceImpl.class);

    @Override
    public RespYedCusToOnlineFinanceBean pushCustomerInfoToOnlineFinance(ReqYedCusToOnlineFinanceBean reqYedCustomerCollectionBean,String gloNo) throws ESBException {

        logger.info("获取客户维护信息，参数为：" + JSON.toJSONString(reqYedCustomerCollectionBean));
        RespYedCusToOnlineFinanceBean respYedCusToOnlineFinanceBean = null;

        EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                anEsbBaseBean().
                withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(gloNo).build();
        EsbRespServiceBean esbRespServiceBean = ESBRequestBuilder.buildClient().
                withEsbBaseBean(esbBaseBean)
                .withBody(reqYedCustomerCollectionBean)
                .withTradeNo(ESBConstant.TRADE_CODE_20120001)
                .withScene(ESBConstant.SCENE_NO_07)
                .create()
                .sendAndReceive();

        String retCode = "";
        // 通过系统头判断是否成功系统头
        EsbRespSysHeadBean sysHead =
                esbRespServiceBean.getSysHead();
        if (Objects.nonNull(sysHead)) {
            // 返回信息数据不为空
            EsbRespRetInfBean esbRespRetInfBean = sysHead.getRetInfArry().stream().findFirst().orElseGet(EsbRespRetInfBean::new);
            retCode = Optional.ofNullable(esbRespRetInfBean.getRetCd()).orElse(StringUtils.EMPTY);
            if (ESBConstant.SUCCESS.equals(retCode)) {
                String msg = "调用客户信息维护接口成功";
                logger.debug(msg);
                respYedCusToOnlineFinanceBean = esbRespServiceBean.getBody(RespYedCusToOnlineFinanceBean.class);
            } else {
                String msg = "调用客户信息维护接口调用失败 " + esbRespRetInfBean.getRetMsg();
                logger.error(msg);
                throw new ESBException(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),msg);
            }
        }
        return respYedCusToOnlineFinanceBean;
    }
}
